'''
Created on 01/04/2010

@author: miguel
'''
import pygame
import random
from EnemyPanel import EnemyPanel
from BugExit import BugExit
from Instruction import Instruction
from Bug import Bug
from GameBoard import GameBoard
from Box import Box
from Tree import Tree
from ComputerBrain import ComputerBrain

class Computer(object):
    (ALIVE, DEAD) = range(2)

    def __init__(self, screen, game, field):
        self.screen = screen
        self.game = game
        self.field = field

        '''create player panel'''
        self.enemy_panel = EnemyPanel(self.screen, self.game)
        '''create bug exit'''
        self.bug_exits = pygame.sprite.Group()
        self.bug_exits.add(BugExit(self.screen, self.game, self.field, BugExit.COMPUTER))
        '''initialize the user boxes group (empty)'''
        self.com_boxes = pygame.sprite.Group()
        '''initialize the user bugs group (empty)'''
        self.com_bugs = pygame.sprite.Group()
        '''number of bugs'''
        self.bugs_number = 0
        self.__init_com_boxes_com_trees()
        self.status = self.ALIVE
        self.supports_discovered = 0
        self.surprises_discovered = 0

        self.brain = ComputerBrain(self.screen, self.game, self.field)

    def support_discovered(self):
        self.supports_discovered = self.supports_discovered + 1
        self.enemy_panel.set_support_discovered()

    def surprise_discovered(self):
        self.surprises_discovered = self.surprises_discovered + 1
        self.enemy_panel.set_surprise_discovered()

    def update(self, time_passed):
        if self.supports_discovered == 3:
            self.status = self.DEAD

        for com_bug in self.com_bugs:
            com_bug.update(time_passed)

        self.brain.update(self.supports_discovered, self.surprises_discovered, self.game.player.supports_discovered, self.game.player.surprises_discovered, self.bugs_number)

        self.enemy_panel.update()

    def __init_com_boxes_com_trees(self):

        self.boxes_list = [Box.SUPPORT, Box.SUPPORT, Box.SUPPORT, Box.SURPRISE2, Box.SURPRISE1]
        self.box_estructure = [[(0, 0), (90, 60), (210, 30), (390, 30), (540, 0)],
                               [(30, 60), (90, 0), (330, 30), (450, 60), (540, 0)],
                               [(60, 0), (120, 60), (330, 0), (450, 30), (540, 30)],
                               [(0, 30), (150, 60), (330, 30), (360, 0), (480, 30)]]
        self.tree_estructure = [[(0, 30), (90, 90), (210, 60), (390, 60), (540, 30)],
                                [(30, 90), (90, 30), (330, 60), (450, 90), (540, 30)],
                                [(60, 30), (120, 90), (330, 30), (450, 60), (540, 60)],
                                [(0, 60), (150, 90), (330, 60), (360, 30), (480, 60)]]

        self.estructure_selected = random.randint(0, len(self.box_estructure) - 1)
        for object in self.boxes_list:
            rand = random.randint(0, len(self.box_estructure[self.estructure_selected]) - 1)
            pos = self.box_estructure[self.estructure_selected][rand]
            self.com_boxes.add(Box(self.screen, self.game, self.field, pos, object, GameBoard.COM_BOX))
            self.box_estructure[self.estructure_selected].pop(rand)

        for object in self.tree_estructure[self.estructure_selected]:
            self.game.trees.add(Tree(self.screen, self.game, self.field, object))


        #self.com_boxes.add(Box(self.screen, self.game, self.field, (180, 60), Box.SUPPORT, GameBoard.COM_BOX))
        #self.com_boxes.add(Box(self.screen, self.game, self.field, (150, 60), Box.SUPPORT, GameBoard.COM_BOX))
        #self.com_boxes.add(Box(self.screen, self.game, self.field, (120, 60), Box.SUPPORT, GameBoard.COM_BOX))
        #self.com_boxes.add(Box(self.screen, self.game, self.field, (390, 60), Box.SURPRISE1, GameBoard.COM_BOX))
        #self.com_boxes.add(Box(self.screen, self.game, self.field, (420, 60), Box.SURPRISE2, GameBoard.COM_BOX))


        #self.game.trees.add(Tree(self.screen, self.game, self.field, (180, 90)))
        #self.game.trees.add(Tree(self.screen, self.game, self.field, (150, 90)))
        #self.game.trees.add(Tree(self.screen, self.game, self.field, (120, 90)))
        #self.game.trees.add(Tree(self.screen, self.game, self.field, (390, 90)))
        #self.game.trees.add(Tree(self.screen, self.game, self.field, (420, 90)))